<?php
/**
 * @author yuanyitian
 * @date 2023/6/19 20:31
 */

namespace utils;

class base64_to_file
{
    public static function get($name, $file_name, $file)
    {
        if (strstr($file, ",")) {
            $file = explode(",", $file);
            $file = $file[1];
        }

        $file_name = explode(".", $file_name);
        if ($name == "") {
            $name = mt_rand(100000, 999999);
        }
        $file_name = end($file_name);

        $path = tempnam(sys_get_temp_dir(), $name . time());
        $handle = fopen($path, "w+");
        fwrite($handle, base64_decode($file));
        fclose($handle);
        $res = [];
        $res["name"] = $name . time() . "." . $file_name;
        $res["path"] = $path;
        return $res;

    }

    //任意文件解码并存到服务器缓存
    public static function getAll($name, $file_name, $file,$size)
    {
        if (strstr($file, ",")) {
            $file = explode(",", $file);
            $file = $file[1];
        }

        $file_name = explode(".", $file_name);
        if ($name == "") {
            $name = mt_rand(100000, 999999);
        }
        $file_name = end($file_name);

        $path = tempnam(sys_get_temp_dir(), $name . time());
        $handle = fopen($path, "w+");
        fwrite($handle, base64_decode($file));
        fclose($handle);
        return [
            'name' => $name . time() . "." . $file_name,
            'size'=>$size,
            'path' => $path
        ];

    }

    public static function binary_get($name,$file)
    {
        $path = tempnam(sys_get_temp_dir(), "qrcode_".$name . time());
        $handle = fopen($path, "w+");
        fwrite($handle,$file);
        fclose($handle);
        $res = [];
        $res["name"] = $name . time() . "." . "png";
        $res["path"] = $path;
        return $res;

    }

    public static function get_from_wechat($file_name, $file)
    {
        $path = tempnam(sys_get_temp_dir(), $file_name);
        $handle = fopen($path, "w+");
        fwrite($handle, base64_decode($file));
        fclose($handle);
        $res = [];
        $res["name"] = $file_name;
        $res["path"] = $path;
        return $res;

    }

    public static function get_image($file)
    {
        $name=time().rand(1000,9999);
        $file_name=$name.".".explode("/",explode(";",$file)[0])[1];
        $path = tempnam(sys_get_temp_dir(), $file_name);
        $handle = fopen($path, "w+");
        fwrite($handle, base64_decode(explode(",",$file)[1]));
        fclose($handle);
        $res = [];
        $res["name"] = $file_name;
        $res["path"] = $path;
        return $res;

    }

    public static function set_file($name, $content)
    {

        $path = tempnam(sys_get_temp_dir(), $name);
        $handle = fopen($path, "w+");
        fwrite($handle, $content);
        fclose($handle);
        $res = [];
        $res["name"] = $name;
        $res["path"] = $path;
        return $res;


    }

}